<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\27 0027
 * Time: 17:53
 */

namespace app\index\doc;


class DocxApi
{
    public $method;
    public $version=0;
    public $ApiInfo = [];
    public $editInfo = [];
    public $dataArray = [];

    public function __construct()
    {
        $this->readJson();
    }

    public function getApiNameByMethod()
    {
        if (!empty($this->dataArray)) {
            $this->ApiInfo = [];
            if ($this->method) {
                //如果不传版本号或者传递错误的版本号以及不存在的版本号，则默认取高版本的版本号
                $apiInfo = [];
                foreach ($this->dataArray as $value) {
                    if($value['method']==$this->method){
                        if(($this->version!=0)&&$this->version==$value['version']){
                            $apiInfo=$value;
                            break;
                        }
                        if($apiInfo){
                            $version = $this->getApiLevel($apiInfo['version'],$value['version']);
                            if($version==$value['version']){
                                $apiInfo=$value;
                            }
                        }else{
                            $apiInfo=$value;
                        }
                    }
                }
                $this->ApiInfo=$apiInfo;
            }
        }
    }
    public function getApiLevel($version,$version_new){
        $string = explode(".",$version);
        $string_new = explode(".",$version_new);
        $level = count($string)>count($string_new)?count($string):count($string_new);
        $versionSn = false;
        for($i=0;$i<$level;$i++){
            if($string[$i]>$string_new[$i]){
                $versionSn =  $version;
            }else{
                $versionSn =  $version_new;
            }
        }
        return $versionSn;
    }

    public function getApiNameList()
    {

    }

    public function addApiName()
    {

    }

    public function editApiName()
    {

    }

    protected function readJson()
    {
        $api = new ApiName();
        $this->dataArray = $api->dataArray;
    }
}